<?php

namespace App\Models;

use App\Enums\LayerPosition;
use App\Enums\LayerType;
use App\Models\Traits\BelongsToApplication;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class Design extends Model implements Sortable
{
    use BelongsToApplication,
        Cachable,
        SortableTrait,
        SoftDeletes;

    protected $casts = [
        'config' => 'json',
        'type' => LayerType::class,
        'position' => LayerPosition::class,
    ];

    protected array $sortable = [
        'order_column_name' => 'order',
        'sort_when_creating' => true,
    ];

    public function buildSortQuery(): Builder
    {
        return static::query()
            ->where('application_id', $this->application_id)
            ->where('season_id', $this->season_id);
    }

    public function season(): BelongsTo
    {
        return $this->belongsTo(Season::class);
    }
}
